package o;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

public class fiy extends Fragment {
    Set<Integer> ˎ = new HashSet();

    public static Fragment ॱ(Activity activity, int i) {
        FragmentManager fragmentManager = activity.getFragmentManager();
        Fragment findFragmentByTag = fragmentManager.findFragmentByTag("com.huawei.hmf.report_fragment_tag");
        if (findFragmentByTag == null) {
            if (Looper.myLooper() != Looper.getMainLooper()) {
                findFragmentByTag = ˊ(fragmentManager);
            } else {
                findFragmentByTag = ˋ(fragmentManager);
            }
        }
        if (findFragmentByTag instanceof fiy) {
            ((fiy) findFragmentByTag).ˎ.add(Integer.valueOf(i));
        }
        return findFragmentByTag;
    }

    private static Fragment ˊ(final FragmentManager fragmentManager) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        final AtomicReference atomicReference = new AtomicReference();
        try {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                public void run() {
                    Object findFragmentByTag = fragmentManager.findFragmentByTag("com.huawei.hmf.report_fragment_tag");
                    if (findFragmentByTag == null) {
                        findFragmentByTag = fiy.ˋ(fragmentManager);
                    }
                    atomicReference.set(findFragmentByTag);
                    countDownLatch.countDown();
                }
            });
            countDownLatch.await();
        } catch (InterruptedException e) {
        }
        return (Fragment) atomicReference.get();
    }

    private static Fragment ˋ(FragmentManager fragmentManager) {
        Fragment o_fiy;
        Exception e;
        try {
            o_fiy = new fiy();
            try {
                fragmentManager.beginTransaction().add(o_fiy, "com.huawei.hmf.report_fragment_tag").commitAllowingStateLoss();
                fragmentManager.executePendingTransactions();
            } catch (Exception e2) {
                e = e2;
                Log.e("ActivityResultFragment", "create fragment failed." + e.getMessage());
                return o_fiy;
            }
        } catch (Exception e3) {
            Exception exception = e3;
            o_fiy = null;
            e = exception;
            Log.e("ActivityResultFragment", "create fragment failed." + e.getMessage());
            return o_fiy;
        }
        return o_fiy;
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        this.ˎ.remove(Integer.valueOf(i));
        fiu.ˊ().ˏ(getActivity(), i, i2, intent);
    }

    public void onDestroy() {
        super.onDestroy();
        if (getActivity() != null && getActivity().isFinishing()) {
            for (Integer intValue : this.ˎ) {
                fiu.ˊ().ॱ(intValue.intValue());
            }
            this.ˎ.clear();
        }
    }
}
